package com.uniview.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.uniview.dao.ResourceRoleDao;
import com.uniview.dao.RoleDao;
import com.uniview.dao.UserRoleDao;
import com.uniview.entity.po.ResourceRole;
import com.uniview.entity.po.Role;
import com.uniview.entity.po.UserRole;
import com.uniview.entity.vo.Pager;
import com.uniview.entity.vo.QueryBuilder;
import com.uniview.entity.vo.ResultMessage;
import com.uniview.service.RoleService;

/**
 * 角色业务接口实现类
 * 
 * @author Qingtian Lei
 *
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {

	Logger logger = Logger.getLogger(RoleServiceImpl.class);

	@Autowired
	protected RoleDao roleDao;

	@Autowired
	protected UserRoleDao userRoleDao;
	
	@Autowired
	protected ResourceRoleDao resourceRoldDao;

	@Override
	public int saveRole(Role role) {
		try {
			return roleDao.saveRole(role);
		} catch (Exception e) {
			logger.error("新增角色信息异常", e);
		}
		return 0;
	}

	@Override
	public int addUserRole(UserRole ur) {
		try {
			return userRoleDao.saveUserRole(ur);
		} catch (Exception e) {
			logger.error("用户授权异常", e);
		}
		return 0;
	}

	@Override
	public boolean updateRole(Role role) {
		try {
			return roleDao.updateRole(role);
		} catch (Exception e) {
			logger.error("修改角色信息异常", e);
		}
		return false;
	}

	@Override
	public boolean deleteRole(int rId) {
		try {
			return roleDao.deleteRole(rId);
		} catch (Exception e) {
			logger.error("删除角色信息异常", e);
		}
		return false;
	}

	@Override
	public boolean deleteUserRole(int[] users) {
		try {
			if (users.length > 0) {
				for (int i = 0; i < users.length; i++) {
					userRoleDao.deleteUserRole(users[i]);
				}
				return true;
			}
		} catch (Exception e) {
			logger.error("用户授权异常", e);
		}
		return false;
	}

	@Override
	public List<Role> findAllRole() {
		try {
			return roleDao.findAllRole();
		} catch (Exception e) {
			logger.error("查询角色列表异常", e);
		}
		return null;
	}

	@Override
	public Pager<Role> findRoleByPager(QueryBuilder queryBuilder, int pageSize, int pageNo) {
		try {
			return roleDao.findRoleByPage(queryBuilder, pageNo, pageSize);
		} catch (Exception e) {
			logger.error("分页查询角色列表异常", e);
		}
		return null;
	}

	@Override
	public Long getTotalRow() {
		try {
			return roleDao.getTotalRow();
		} catch (Exception e) {

		}
		return null;
	}

	@Override
	public List<UserRole> findUserRoleByRId(int rId) {
		try{
			return userRoleDao.findAllUserRoleByRid(rId);
		}catch(Exception e){
			logger.error("查询授权列表异常", e);
		}
		return null;
	}

	@Override
	public Pager<UserRole> findUserRoleByPager(QueryBuilder queryBuilder, int pageSize, int pageNo) {
		try{
			return userRoleDao.findUserRoleByPager(queryBuilder, pageSize, pageNo);
		}catch(Exception e){
			logger.error("分页查询授权列表异常", e);
		}
		return null;
	}

	@Override
	public Long getUserRoleCounts() {
		try{
			return userRoleDao.getTotalRow();
		}catch(Exception e){
			logger.error("获取授权用户总数异常", e);
		}
		return null;
	}

	@Override
	public Role findRoleById(int roleId) {
		try{
			return roleDao.findRoleById(roleId);
		}catch(Exception e){
			logger.error("获取角色信息异常", e);
		}
		return null;
	}

	@Override
	public ResultMessage saveResoucreRole(List<ResourceRole> list, int roleId) {
		try {
			try {
				resourceRoldDao.deleteResouceRoleWithRoleId(roleId);
			} catch (Exception e) {
				logger.error("删除角色权限异常");
				logger.error(e);
				return new ResultMessage("保存角色权限失败", false);
			}
			resourceRoldDao.addResouceRole(list);
			return new ResultMessage("保存角色权限成功", true);
		} catch (Exception e) {
			logger.error("保存角色权限异常");
			logger.error(e);
		}
		return new ResultMessage("保存角色权限失败", false);
	}

	@Override
	public boolean saveUserRole(List<UserRole> list) {
		try {
			return userRoleDao.saveUserRole(list);
		} catch (Exception e) {
			logger.error("保存用户授权异常", e);
		}
		return false;
	}

	@Override
	public boolean checkUserRole(String userId) {
		try {
			return userRoleDao.checkUserRole(userId);
		} catch (Exception e) {
			logger.error("检查用户授权异常", e);
		}
		return false;
	}

	@Override
	public List<ResourceRole> findReousceRoleByUserId(String userId) {
		try {
			return resourceRoldDao.findReousceRoleByUserId(userId);
		} catch (Exception e) {
			logger.error("查询用户授权资源异常", e);
		}
		return null;
	}

	@Override
	public UserRole findRecouseRoleByWorkNo(String workNo, int roleId) {
		try{
			String hql= "from  UserRole where userId =upper('"+workNo+"') and roleId="+roleId;
			List<UserRole> urlist =userRoleDao.findByHql(hql);
			if(urlist !=null && urlist.size() >0){
				return urlist.get(0);
			}
		}catch(Exception e){
			logger.error("查询用户授权资源异常", e);
		}
		return null;
	}

}
